package tech.runchen.gz5055.oms.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lrinfo.fpcore;
import org.springframework.stereotype.Service;
import tech.runchen.gz5055.oms.modules.system.entity.FingerprintEntity;
import tech.runchen.gz5055.oms.modules.system.mapper.FingerprintMapper;
import tech.runchen.gz5055.oms.modules.system.service.IFingerprintService;

import java.util.List;
import java.util.Map;

/**
 * @author zzj
 */
@Service
public class FingerprintServiceImpl extends ServiceImpl<FingerprintMapper, FingerprintEntity> implements IFingerprintService {
    @Override
    public List<FingerprintEntity> queryList(Map<String, Object> params) {
        List<FingerprintEntity> list = this.list();
        return list;
    }

    @Override
    public boolean saveFingerprint(FingerprintEntity fingerprint) {
        return this.save(fingerprint);
    }

    @Override
    public boolean updateFingerprint(FingerprintEntity fingerprint) {
        return this.updateById(fingerprint);
    }

    @Override
    public void deleteFingerprint(Long fingerprintId) {
        this.removeById(fingerprintId);
    }

    @Override
    public Long checkFingerprint(String psChar) {
        fpcore jc = new fpcore();
        List<FingerprintEntity> list = this.list(new QueryWrapper<FingerprintEntity>().orderByDesc("frequency"));
        for (FingerprintEntity fingerprint : list) {
            if (jc.MatchTemplate(psChar.getBytes(), fingerprint.getPsChar().getBytes()) >= 60) {
                fingerprint.setFrequency(fingerprint.getFrequency() + 1);
                this.updateFingerprint(fingerprint);
                return fingerprint.getUserId();
            }
        }
        return null;
    }
}
